jQuery(function(){
	jQuery.noConflict();
	var orderStatus = localStorage.getItem("orderStatus");
	var shipStatus = localStorage.getItem("shipStatus");
	var daifu = localStorage.getItem('daifu');
	var order = localStorage.getItem('order');
	if(daifu != null && order !=null){
		localStorage.removeItem('daifu');
		localStorage.removeItem('order');
		$('#daifu').click();
		$('#myOrder').click();
	}
	var daifa = localStorage.getItem('daifa');
	var order1 = localStorage.getItem('order1');
	if(daifa !=null && order1 !=null){
		localStorage.removeItem('daifa');
		localStorage.removeItem('order1');
		$('#daifa').click();
		$('#myOrder').click();
	}
	var nav_list = Zepto("#nav_list li");
	var sec = Zepto("section");
	nav_list.bind("click",function(){
		Zepto(this).siblings('li').css({"color":"#9f9f9f"});
		Zepto(this).css({"color":"#89bafe"});
		sec.css({"display":"none"})
		sec.eq(Zepto(this).index()).css({"display":"block"});
		if($(this).index()==1){
			if(null==orderStatus&&null==shipStatus){
				initData("","");
			}
		}
	});
	if(null != orderStatus && null !=shipStatus){
		localStorage.removeItem("orderStatus");
		localStorage.removeItem("shipStatus");
		initData(orderStatus,shipStatus);
	}
	var bl = localStorage.getItem("sellerOrder");
	if(null != bl){
		localStorage.removeItem("sellerOrder");
		bl = null;
		$('.icon-dingdan').parent().click();
	}
	var bs = localStorage.getItem("shipment");
	if(null != bs){
		localStorage.removeItem("shipment");
		bs = null;
		$('.icon-dingdan').parent().click();
	}
	var bc = localStorage.getItem("cancel");
	if(null != bc){
		localStorage.removeItem("cancel");
		bc = null;
		$('.icon-dingdan').parent().click();
	}
	var bo = localStorage.getItem("orderList");
	if(null != bo){
		localStorage.removeItem("orderList");
		bo = null;
		$('.icon-dingdan').parent().click();
	}
});
function initData(orderStatus,shipStatus){
	jQuery("#art").children().remove();
	jQuery.ajax({
		url:server_url+"/rest/sellerOrder/getOrders.do",
		data:{sellerUserId:userid,orderStatus:orderStatus,shipStatus:shipStatus},
		success:function(data){
			//console.log(data);
			if(data.data.length>0){
				mapIfExist(function(order){
					//console.log(order);
					var status=(order.orderStatus==='01' || order.orderStatus==='05')?order.orderStatusName:order.shipStatusName;
					var detailHtml = _.map(order.lstOrderDetail,function(detail){
						//console.log(detail);
						return '<div class="ware" onclick="toOrderDetail(\''+order.id+'\',\''+orderStatus+'\',\''+shipStatus+'\')">\
										<h4 class="clear">\
										<a href="javascript:;">'+detail.productName+" "+detail.spec+'</a>\
										<span>'+detail.productPrice+' RMB/头</span>\
									</h4>\
									<p class="proNumber clear">\
										<span>'+parseFloat(detail.productNum)+'头</span>\
									</p>\
								</div>';
					}).join("");
					var operation='';
					if(order.orderStatus==='01' && (!exist(order.shipStatus) || order.shipStatus === '00')){
						operation='<aside class="mess">\
							<span><a href="tel:'+order.userMobile+'">电话买家</a></span>\
							<span class="cancel" orderid='+order.id+' productNum='+order.lstOrderDetail[0].productNum+' skuId='+order.lstOrderDetail[0].supplyProductID+'>取消订单</span>\
						</aside>';
					}
					if(order.shipStatus === '01'){
						operation = '<aside class="mess">\
							<span><a href="tel:'+order.userMobile+'">电话买家</a></span>\
							<span class="ship" onclick="shipment(\''+order.id+'\')">发货</span>\
						</aside>';
					}
					jQuery('<div class="d_order" >\
							<h3>\
								<a href="javascript:;">'+order.buyerName+'</a>\
								<span>'+status+'</span>\
							</h3>'+
							detailHtml+
							'<p class="all"><span>合计：￥'+order.price+'</span></p>'+
							operation+
							'<p class="split"></p>\
						</div>').appendTo(jQuery("#art"));
				}, data.data);
			}else{
				if(orderStatus == "" && shipStatus == ""){
					jQuery("<div>"+
							"<p style='font-size:0.35rem; padding:50% 5% 50% 5%; text-align:center;'>您暂时还没有交易订单!</p>"+
							"</div>").appendTo(Zepto("#art"));
				}
			
			}
			
			initPage(orderStatus,shipStatus);
		}
	});
}
function toOrderDetail(orderid,a,b){
	//localStorage.setItem("orderId",orderid );
	localStorage.setItem('orderStatus',a);
	localStorage.setItem('shipStatus',b);
	window.location.href=server_url+"sellerOrder/detail.do?orderid="+orderid;
}
function initPage(orderStatus,shipStatus){
	var state = Zepto("#state p");
	if(orderStatus =='02'&&shipStatus =='03'){
		Zepto(state[4]).siblings('p').removeClass("select");
		Zepto(state[4]).addClass("select");
	}else if(orderStatus ==''&&shipStatus =='01'){
		Zepto(state[3]).siblings('p').removeClass("select");
		Zepto(state[3]).addClass("select");
	}else if(orderStatus =='02'&&shipStatus =='02'){
		Zepto(state[2]).siblings('p').removeClass("select");
		Zepto(state[2]).addClass("select");
	}else if(orderStatus =='01'&&shipStatus ==''){
		Zepto(state[1]).siblings('p').removeClass("select");
		Zepto(state[1]).addClass("select");
	}else{
		Zepto(state[0]).siblings('p').removeClass("select");
		Zepto(state[0]).addClass("select");
	}
		
	var nav_list = Zepto("#nav_list li");
	var sec = Zepto("section");
	nav_list.bind("click",function(){
		Zepto(this).siblings('li').css({"color":"#9f9f9f"});
		Zepto(this).css({"color":"#89bafe"});
		sec.css({"display":"none"})
		sec.eq(Zepto(this).index()).css({"display":"block"});
		
	});
	Zepto(".cancel").tap(function(){
		Zepto("#wrap_order").css({"display":"block"})
		jQuery("#cancelOrderId").val(jQuery(this).attr("orderid"));
		jQuery("#overPlusNum").val(jQuery(this).attr("productNum"));
		jQuery("#skuId").val(jQuery(this).attr("skuId"));
	});
	Zepto("#close_icon").tap(function(){
		Zepto("#wrap_order").css({"display":"none"})
	});
	
	
	Zepto("#problem_text").tap(function(){
		Zepto("#problem").css({"display":"block"})
	});
	Zepto("#close_text").tap(function(){
		Zepto("#problem").css({"display":"none"})
	});
	Zepto("#hold").tap(function(){
		Zepto("#problem").css({"display":"none"})
	});
	
	
}
function cancel(){
	localStorage.setItem("cancel",1);
	var orderid = jQuery("#cancelOrderId").val();
	var skuNum = jQuery("#overPlusNum").val();
	var id = jQuery("#skuId").val();
	var cancelReason = jQuery("#cancelReason").val();
	if(empty(cancelReason)){
		easyAlert("取消原因不能为空");
		return;
	}
	if(cancelReason.lenth > 280){
		easyAlert("取消原因不能超过140个字");
		return;
	}
	$.ajax({
		type:"post",
		url:server_url+"/rest/sellerOrder/cancelOrder.do",
		data:{userid:userid,orderid:orderid,cancelReason:cancelReason,skuId:id,overPlusNum:skuNum,ran:Math.random()},
		success:function(){
			easyAlert("取消成功");
			window.location.href=server_url+"index.do";
		}
	});
	
}
function shipment(orderid){
	localStorage.setItem("shipment",1);
	window.location.href=server_url+"sellerOrder/shipment.do?orderid="+orderid;
}